package com.andyage.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

/**
 * 短信控制
 * 对接的是中国建网的
 * @author liuyayun
 *
 */
public class SMSServerJanWang {
	/**
	 * 中国建网短息服务平台发送地址
	 */
	private static String url = MyConfig.SMSServerJanWang_url;
	/**
	 * 在中国建网的用户名
	 */
	private static String user = MyConfig.SMSServerJanWang_user;
	/**
	 * 中国建网密码
	 */
	private static String password = "";
	/**
	 * 中国建网key
	 */
	private static String key = MyConfig.SMSServerJanWang_key;
	/**
	 * 日志打印
	 */
	private static Logger m_logger = Logger.getLogger(SMSServerJanWang.class); 
	/**
	 * 发送短信服务
	 * 一次最多支持100个
	 * @param tel
	 * @param message
	 * @return
	 */
	public static boolean smsServer(String tel,String message)
	{
		boolean result = false;
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(url); 
		post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
		NameValuePair[] data ={ new NameValuePair("Uid", user),new NameValuePair("Key", key),new NameValuePair("smsMob",tel),new NameValuePair("smsText",message)};
		post.setRequestBody(data);
		try {
			client.executeMethod(post);
		} catch (Exception e) {
			
		}
		
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		m_logger.info("statusCode:"+statusCode);
		for(Header h : headers)
		{
			m_logger.info(h.toString());
		}
		try {
			String xiaoxi = new String(post.getResponseBodyAsString().getBytes("gbk")); 
			m_logger.info("短信服务返回消息状态："+xiaoxi); //打印返回消息状态
		} catch (Exception e) {
			
		}
		post.releaseConnection();
		return result;
	}
	/**
	 * 发送短信服务
	 * 一次最多支持100个
	 * @param tels
	 * @param message
	 * @return
	 */
	public static boolean smsServer(String[] tels,String message)
	{
		String telStr = "";
        for (int i = 0; i < tels.length; i++)
        {
            String tel = tels[i];
            if (isRightSMS(tel))
            {
                telStr += tel + ",";
            }
        }
        if (telStr.endsWith(","))
        {
        	
            telStr = telStr.substring(0,telStr.length()-1);
        }

        return smsServer(telStr,message);
	}
	/**
	 * 发送短信服务
	 * 支持任意多个
	 * @param tel
	 * @param message
	 * @return
	 */
	 public static Boolean smsServerLong(String tel, String message)
     {
         String[] tels = tel.split(",");
         return smsServerLong(tels,message);
     }
	/**
	 * 发送短信服务
	 * 支持任意多个
	 * @param tels
	 * @param message
	 * @return
	 */
	public static Boolean smsServerLong(String[] tels, String message)
    {
        Boolean result = true;
        String telStr = "";
        for (int i = 0; i < tels.length; i++)
        {
            String tel = tels[i];
            if (isRightSMS(tel))
            {
                telStr += tel + ",";
            }
            
            //满100个的时候发送一次，然后清空
            if ((i + 1) % 100 == 0)
            {
                if (telStr.endsWith(","))
                {
                	telStr = telStr.substring(0,telStr.length()-1);
                }
                Boolean isS = smsServer(telStr, message);
                if (!isS)
                {
                    result = false;
                }
                telStr = "";
            }
        }
        //最后的一次发送
        if (telStr.endsWith(","))
        {
        	telStr = telStr.substring(0,telStr.length()-1);
        }
        Boolean isZ = smsServer(telStr, message);
        if (!isZ)
        {
            result = false;
        }
        return result;
    }
	public static Boolean smsServerLong(Object[] tels, String message)
    {
        Boolean result = true;
        String telStr = "";
        for (int i = 0; i < tels.length; i++)
        {
            String tel = (String)tels[i];
            if (isRightSMS(tel))
            {
                telStr += tel + ",";
            }
            
            //满100个的时候发送一次，然后清空
            if ((i + 1) % 100 == 0)
            {
                if (telStr.endsWith(","))
                {
                	telStr = telStr.substring(0,telStr.length()-1);
                }
                Boolean isS = smsServer(telStr, message);
                if (!isS)
                {
                    result = false;
                }
                telStr = "";
            }
        }
        //最后的一次发送
        if (telStr.endsWith(","))
        {
        	telStr = telStr.substring(0,telStr.length()-1);
        }
        Boolean isZ = smsServer(telStr, message);
        if (!isZ)
        {
            result = false;
        }
        return result;
    }
	
	/**
	 * 判定手机号是否正确
	 * @param tel
	 * @return
	 */
	public static boolean isRightSMS(String tel)
	{
		boolean result = false;
//		String regExp = "^[1]([3][0-9]{1}|59|58|88|89|86)[0-9]{8}$";  138
		String regExp="1([\\d]{10})|((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}";//207
//		String regExp="^1[3|4|5|8][0-9]\\d{8}$";//202
		  
		Pattern p = Pattern.compile(regExp);  
	  
		Matcher m = p.matcher(tel);  
	  
		if(m.find())
		{
			result = true;
		}
		return result;
	}
}
